

package com.maoshi.shop.distribution.api.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.maoshi.shop.common.config.Constant;
import com.maoshi.shop.common.exception.MaoshiShopBindException;
import com.maoshi.shop.common.util.PageParam;
import com.maoshi.shop.distribution.comment.dto.AchievementDataDto;
import com.maoshi.shop.distribution.comment.dto.DistributionOrderDto;
import com.maoshi.shop.distribution.comment.dto.DistributionUserDto;
import com.maoshi.shop.distribution.comment.dto.DistributionUserSimpleDto;
import com.maoshi.shop.distribution.comment.model.DistributionLevel;
import com.maoshi.shop.distribution.comment.model.DistributionLevelSet;
import com.maoshi.shop.distribution.comment.model.DistributionMsg;
import com.maoshi.shop.distribution.comment.model.DistributionUser;
import com.maoshi.shop.distribution.comment.service.*;
import com.maoshi.shop.security.api.util.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import ma.glasnost.orika.MapperFacade;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

@RestController
@RequestMapping("/p/distribution/user")
@Api(tags = "分销员接口")
@AllArgsConstructor
public class DistributionUserController {

    private final DistributionUserService distributionUserService;

    private final DistributionUserIncomeService distributionUserIncomeService;

    private final DistributionLevelService distributionLevelService;

    private final DistributionLevelSetService distributionLevelSetService;

    private final MapperFacade mapperFacade;

    @GetMapping("/info")
    @ApiOperation(value = "查看分销中心信息")
    public ResponseEntity<AchievementDataDto> info(@RequestParam(value = "shopId",required = false,defaultValue = "0") Long shopId) {
//        Long shopId = 0L;
        DistributionUser distributionUser = distributionUserService.getByUserIdAndShopId(SecurityUtils.getUser().getUserId(), shopId);
        if (Objects.isNull(distributionUser)) {
            throw new MaoshiShopBindException("您还不是销售员");
        }
        return ResponseEntity.ok(distributionUserService.getAchievementDataDtoById(distributionUser.getDistributionUserId()));
    }

    @GetMapping("/distributionUserOrders")
    @ApiOperation(value = "分页查看分销员佣金明细", notes = "传入分销员id")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderBy", value = "时间排序(0升序 1降序) 默认1", required = false, dataType = "Integer")
    })
    public ResponseEntity<IPage<DistributionOrderDto>> getDistributionOrderDtosByDistributionUserId(
            PageParam<DistributionOrderDto> page
    ) {
        Long shopId = Constant.PLATFORM_SHOP_ID;
        DistributionUser distributionUser = distributionUserService.getByUserIdAndShopId(SecurityUtils.getUser().getUserId(), shopId);
        return ResponseEntity.ok(distributionUserIncomeService.getDistributionOrderDtoByDistributionUserId(page, distributionUser.getDistributionUserId()));
    }

    @GetMapping("/distributionUserInfo")
    @ApiOperation(value = "分销员信息")
    public ResponseEntity<DistributionUserDto> isDistributionUser() {
        Long shopId = Constant.PLATFORM_SHOP_ID;
        DistributionUser distributionUser = distributionUserService.getByUserIdAndShopId(SecurityUtils.getUser().getUserId(), shopId);
        DistributionUserDto distributionUserDto = mapperFacade.map(distributionUser, DistributionUserDto.class);
        if (distributionUserDto == null) {
            return ResponseEntity.ok().build();
        }
        DistributionLevelSet distributionLevelSet = distributionLevelSetService.getByShopId();
        DistributionLevel levelInfo = distributionLevelService.getLevelInfo(distributionUser.getLevel(), distributionLevelSet.getDistributionLevels());
        distributionUserDto.setLevelName(levelInfo.getName());
        return ResponseEntity.ok(distributionUserDto);
    }

    @GetMapping("/subordinate")
    @ApiOperation(value = "查看下级")
    public ResponseEntity<IPage<DistributionUserSimpleDto>> subordinatePage(PageParam<DistributionMsg> page) {
        Long shopId = Constant.PLATFORM_SHOP_ID;
        DistributionUser distributionUser = distributionUserService.getByUserIdAndShopId(SecurityUtils.getUser().getUserId(), shopId);
        return ResponseEntity.ok(distributionUserService.getDistributionUserSimpleDtoByParentUserIdPage(page, distributionUser.getDistributionUserId()));
    }


}
